package com.lidx.back.statistics.controller.admin;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.lidx.back.statistics.commons.ClientAction;
import com.lidx.back.statistics.commons.MaterialAction;
import com.lidx.back.statistics.entity.MaterialRecord;
import com.lidx.back.statistics.service.IClientRecordService;
import com.lidx.back.statistics.service.IMaterialRecordService;
import com.lidx.back.statistics.service.IMaterialService;

@Controller
@RequestMapping("/admin/client")
public class ClientAdminController {
	@Autowired
	private IClientRecordService recordService;
	@Autowired
	private IMaterialRecordService materialService;
	
	@RequestMapping(value="list",method=RequestMethod.GET)
	public String clientPage(){
		return "client/list";
	}
	
	@RequestMapping(value="list",method=RequestMethod.POST)
	public ModelAndView goClientPage(@RequestParam(value="clientRecords") String action,
			@RequestParam String versionType,
			@RequestParam(required=false) String from,
			@RequestParam(required=false) String to){
		ModelAndView mav = new ModelAndView("client/list");
		if(StringUtils.isNotBlank(action) && StringUtils.isNoneBlank(versionType)){
			mav.addObject("action", action);
			mav.addObject("versionType", versionType);
			//if(StringUtils.isNoneBlank(to)&& StringUtils.isNoneBlank(from)){
			if(ClientAction.CLIENT_RECORD_VIDEO.equals(action)){
				
			}
			if(StringUtils.isNotBlank(from)&&StringUtils.isNotBlank(to)){
				mav.addObject("from", from);
				mav.addObject("to", to);
			}
			List<Object[]> list = recordService.getRecordsWithParams(action,versionType,from,to);
			mav.addObject("records", list);
			//}
		}
		
		return mav;
	}
	
	@RequestMapping(value="shareData",method=RequestMethod.GET)
	public String sharePage(){
		return "client/share";
	}
	
	@RequestMapping(value="shareData",method=RequestMethod.POST)
	public ModelAndView goSharePage(@RequestParam(value="clientRecords") String action,
			@RequestParam String versionType,
			@RequestParam(required=false) String from,
			@RequestParam(required=false) String to){
		ModelAndView mav = new ModelAndView("client/share");
		if(StringUtils.isNotBlank(action) && StringUtils.isNoneBlank(versionType)){
			mav.addObject("action", action);
			mav.addObject("versionType", versionType);
			//if(StringUtils.isNoneBlank(to)&& StringUtils.isNoneBlank(from)){
			if(StringUtils.isNotBlank(from)&&StringUtils.isNotBlank(to)){
				mav.addObject("from", from);
				mav.addObject("to", to);
			}
			if(MaterialAction.MATERIAL_PIC_SHARE.equals(action) || MaterialAction.MATERIAL_VIDEO_SHARE.equals(action)){
				List<Object[]> list = materialService.getRecordsWithChannel(action,versionType,from,to);
				mav.addObject("records", list);
			}
		}
		
		return mav;
	}
}
